﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Controls.Primitives;
using System.Threading;


namespace ManagementSystem
{
    /// <summary>
    /// Interaction logic for LisaUusTund.xaml
    /// </summary>
    public partial class LisaUusTund : Window
    {
        public LisaUusTund(StatusBar status, User user, string aine)
        {
            InitializeComponent();
            datePicker1.SelectedDate = DateTime.Now;
            this.status = status;
            this.user = user;
            this.aine = aine;
        }
        private StatusBar status;
        private User user;
        private string aine;

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            Tunnike tun = new Tunnike();

            if (topicBox.Text.Equals(""))
            {
                topicBox.BorderBrush = new SolidColorBrush(Colors.Red);
                return;
            }
            tun.Topic = topicBox.Text;

            tun.Date = (DateTime)datePicker1.SelectedDate;

            if (descriptionBox.Text.Equals(""))
            {
                tun.Description = "";
            }
            else
            {
                tun.Description = descriptionBox.Text;
            }

            if (notesBox.Text.Equals(""))
            {
                tun.Comment = "";
            }
            else
            {
                tun.Comment = notesBox.Text;
            }

            tun.User_ID = user.User_ID;
            DataClasses1DataContext dataContext = new DataClasses1DataContext();
            
            var s = from st in dataContext.Subjects where st.Name == aine select st;
            foreach (var st in s){
                tun.Subject_ID = st.Subject_ID;
            }

            Thread workerThread = new Thread(() => this.saveData(tun));
            workerThread.Start();
            this.Close();
        }

        private void saveData(Tunnike tun)
        {
            try
            {
                DataClasses1DataContext kont = new DataClasses1DataContext();
                Lesson les = new Lesson();
                les.Topic = tun.Topic;
                les.Date = tun.Date;
                les.Descripton = tun.Description;
                les.Comment = tun.Comment;
                les.Subject_ID = tun.Subject_ID;
                les.User_ID = tun.User_ID;
                kont.Lessons.InsertOnSubmit(les);
                kont.SubmitChanges();
                kont.Dispose();


                Action action = new Action(() => { status.Items.Clear(); status.Items.Add("Uus tund lisatud !"); });
                this.Dispatcher.Invoke(action, DispatcherPriority.ApplicationIdle);
            }
            catch (Exception)
            {
                MessageBox.Show("Tekkis viga tunni lisamisel ");
            }
        }

        private void topicBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            topicBox.BorderBrush = new SolidColorBrush(Colors.Black);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
